export function getFormByCurrentNodeBefore(businessObject, formList){
  if(!businessObject){
    return formList
  }
  const type = businessObject.$type
  if(type === 'bpmn:StartEvent') {
    if(businessObject.formRef){
      formList.push(businessObject.formRef)
    }
    return formList
  }
  if(type === 'bpmn:UserTask') {
    if(businessObject.formRef){
      formList.push(businessObject.formRef)
    }
    if(businessObject.incoming){
      businessObject.incoming.forEach((item) => {
        return getFormByCurrentNodeBefore(item, formList)
      })
    }
  }
  if(type === 'bpmn:SequenceFlow') {
    return getFormByCurrentNodeBefore(businessObject.sourceRef, formList)
  }
  if(type === 'bpmn:ExclusiveGateway') {
    if(businessObject.incoming){
      businessObject.incoming.forEach((item) => {
        return getFormByCurrentNodeBefore(item, formList)
      })
    }
  }
}

export function createConditionExpression(bpmnFactory, element, commandStack) {
  if(element.businessObject.get('conditionExpression') === undefined) {
    const formalExpression = bpmnFactory.create('bpmn:FormalExpression', {
      body: ''
    });

    const commands = [];

    commands.push({
      cmd: 'element.updateModdleProperties',
      context: {
        element,
        moddleElement: element.businessObject,
        properties: {
          conditionExpression: formalExpression
        }
      }
    });
    commandStack.execute('properties-panel.multi-command-executor', commands);
  }
}
export function deleteConditionExpression(bpmnFactory, element, commandStack) {
  if(element.businessObject.get('conditionExpression') !== undefined) {
    const commands = [];

    commands.push({
      cmd: 'element.updateModdleProperties',
      context: {
        element,
        moddleElement: element.businessObject,
        properties: {
          conditionExpression: undefined
        }
      }
    });
    commandStack.execute('properties-panel.multi-command-executor', commands);
  }
}

export function createExtensionProperties(bpmnFactory, element, commandStack, type, event, class$1){
  if(element.businessObject.get('extensionElements') === undefined){
    const listener = bpmnFactory.create(type, {
      event: event,
      class: class$1
    });

    let extensionElements = bpmnFactory.create('bpmn:ExtensionElements', {
      values: []
    })

    listener.$parent = extensionElements
    const commands = [];

    commands.push({
      cmd: 'element.updateModdleProperties',
      context: {
        element,
        moddleElement: element.businessObject,
        properties: {
          extensionElements
        }
      }
    });
    commands.push({
      cmd: 'element.updateModdleProperties',
      context: {
        element,
        moddleElement: extensionElements,
        properties: {
          values: [...extensionElements.get('values'), listener]
        }
      }
    });
    commandStack.execute('properties-panel.multi-command-executor', commands);
  }
}

export function createUserTaskExtensionProperties(bpmnFactory, element, commandStack){
  if(element.businessObject.get('extensionElements') === undefined){

    const listener = bpmnFactory.create('camunda:Properties', {
      values: []
    });

    let extensionElements = bpmnFactory.create('bpmn:ExtensionElements', {
      values: []
    })

    listener.$parent = extensionElements
    const commands = [];

    commands.push({
      cmd: 'element.updateModdleProperties',
      context: {
        element,
        moddleElement: element.businessObject,
        properties: {
          extensionElements
        }
      }
    });
    commands.push({
      cmd: 'element.updateModdleProperties',
      context: {
        element,
        moddleElement: extensionElements,
        properties: {
          values: [...extensionElements.get('values'), listener]
        }
      }
    });

    const assignee = createElement('camunda:Property', {
      name: 'assignee',
      value: ''
    }, listener, bpmnFactory)
    const auth = createElement('camunda:Property', {
      name: 'auth',
      value: ''
    }, listener, bpmnFactory)
    const condition = createElement('camunda:Property', {
      name: 'condition',
      value: ''
    }, listener, bpmnFactory)

    commands.push({
      cmd: 'element.updateModdleProperties',
      context: {
        element,
        moddleElement: listener,
        properties: {
          values: [...listener.get('values'), assignee, auth, condition]
        }
      }
    });

    commandStack.execute('properties-panel.multi-command-executor', commands);
  }
}

export function createLoopCharacteristics(bpmnFactory, element, commandStack) {
  const businessObject = element.businessObject
  const loopCharacteristics = businessObject.get('loopCharacteristics')
  if(loopCharacteristics !== undefined && loopCharacteristics.get('completionCondition') === undefined){
    loopCharacteristics.collection = "${assigneeList}"
    loopCharacteristics.elementVariable = "assignee"
    let completionConditionCreate = bpmnFactory.create('bpmn:FormalExpression', {
      body: "${nrOfCompletedInstances == nrOfInstances}"
    })
    completionConditionCreate.$parent = loopCharacteristics
    loopCharacteristics.completionCondition = completionConditionCreate
  }
}

/**
 * Create a new element and set its parent.
 *
 * @param {String} elementType of the new element
 * @param {Object} properties of the new element in key-value pairs
 * @param {moddle.object} parent of the new element
 * @param {BpmnFactory} factory which creates the new element
 *
 * @returns {djs.model.Base} element which is created
 */
function createElement(elementType, properties, parent, factory) {
  const element = factory.create(elementType, properties);
  if (parent) {
    element.$parent = parent;
  }
  return element;
}

